<?php
Yii::import('dao.auction.AuctionLiveDao');
class PaymentInfo extends PaymentData{
		
	protected $_paymentData;
	
	public $totalPrice;
	
	public $_cache;
	
	public $giftCard=0;
	
	public function __construct($paymentData = array()){
		
		$this->_cache = Yii::app()->cache;
		$this->_cache->serializer = array(function($val){
			return json_encode($val[0]);
		},function($val){
			return json_decode('['.$val.',null]',true);
		});
		$this->setName('paymentInfo');
		
		if (!empty($paymentData)){
			foreach ($paymentData as $paymentDataObj){
				if ($paymentDataObj instanceof PaymentData){
					$this->setPaymentData($paymentDataObj);
				}
			}
		}
	}

	public function __get($name){
		return isset($this->_paymentData[$name])?$this->_paymentData[$name]:false;
	}

	public function setPaymentData($paymentData){
		if ($paymentData instanceof PaymentData){
			$this->_paymentData[$paymentData->getName()] = $paymentData;
		}
	}
	
	public function delPaymentData($paymentDataName){
		unset($this->_paymentData[$paymentDataName]);
		return $this;
	}

	/**
	 *  判断是否为当前胜利者 
	 */
	public function isWinner($auctionId,$customerId){
	    $dao=new AuctionLiveDao();
	    $dao->find($auctionId);
	    if ($dao->winner_id==$customerId){
	        return true;
	    }
	    return false;
	}
	
	public function vaild(){
        if(empty($this->_paymentData)){
            $this->setErrorMessage('error','payment error!!!');
            return false;
        }
		foreach ($this->_paymentData as $key => $val){
			$tempArray[$key] = array(
				'vaild'		=>	$val->vaild(),
				'message'	=>	$val->errorMessage(),
			);
			
			/**
			 * 检验是否为当前胜利者
			 */
			if($key=='auction'  && $this->isWinner($val->auctionId, Yii::app()->user->id)){
			    $this->setErrorMessage($key, "You are current winner!");
			    return false;
			}
			if (!$val->vaild()){
				$this->setErrorMessage($key, $val->errorMessage());
				return false;
			}
		}
		return true;
	}
	
	public function clearPaymentData(){
		$this->_paymentData = '';
		return $this;
	}
	
	public function toArray(){
		$tempArray = array();
		foreach ($this->_paymentData as $key => $val){			
			$tempArray[$this->getName()]['class'][$key] = $val->getClass();
			$tempArray[$this->getName()]['customerId'] = $this->_customerId;
			$tempArray[$this->getName()]['totalPrice'] = $this->totalPrice;
			$tempArray[$this->getName()]['giftcard'] = $this->giftCard;
			$tempArray[$key] = $val->toArray();
		}
		return $tempArray;
	}
	
	public function saveCache(){
		$this->_cache->set($this->getPaymentCode(),$this->toArray());
		return $this;
	}
	
	public function getCache(){
		$cacheData = $this->_cache->get($this->_paymentCode);	
		if (!empty($cacheData)){
			$paymentData = $cacheData[$this->getName()];
			$this->totalPrice = $paymentData['totalPrice'];
			$this->giftCard = $paymentData['giftcard'];
			unset($cacheData[$this->getName()]);
			foreach ($cacheData as $paymentDataKey => $data){
				if (empty($this->$paymentDataKey)){
					$this->setPaymentData(new $paymentData['class'][$paymentDataKey]);
				}
				foreach ($data as $key => $val){
					$this->$paymentDataKey->$key = $val;
// 					if($paymentDataKey=="buyItNow" and $key=="type"){
// 						var_dump($key,$val,$this->$paymentDataKey->$key);
// 					}
					
				}
			}	
		}
		return $this;
	}

	public function deleteCache(){
		$this->_cache->delete($this->_paymentCode);
		return $this;
	}

	public function existence($dataName){
		return isset($this->_paymentData[$dataName]);
	}

}